import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router';
import { HomeService } from 'src/app/services/home.service';
import { SingerService } from 'src/app/services/singer.service.';
import { HotTag, Banner, SongSheet, Singer } from 'src/app/services/data-type/common.type';
import { Observable, forkJoin } from 'rxjs';
import { first } from 'rxjs/internal/operators';

type HomeDataType = [Banner[], HotTag[], SongSheet[], Singer[]];

@Injectable()
export class HomeResolveService implements Resolve<HomeDataType> {
  constructor(private homeService: HomeService, private singerService: SingerService) {}

  resolve(): Observable<HomeDataType> {
    // 将数组转成 Observable 并且将最新值发射出去
    return forkJoin([
      this.homeService.getBanners(),
      this.homeService.getHotTags(),
      this.homeService.getPersonalSheetList(),
      this.singerService.getEnterSinger()
    ]).pipe(first());
  }
}
